#!/bin/sh
################################################################################
# udhcpc.script
################################################################################

/usr/bin/test -r /etc/udhcpc.conf && . /etc/udhcpc.conf

/usr/bin/test "${MM_DEBUG}" = "yes" && set -x

DHCP_CONF='/etc/conf.d/dhcp'
HOSTS_CONF='/etc/hosts'
LOG_CONF='/etc/log.conf'
NTP_CONF='/etc/ntp.conf'
RESOLV_CONF='/etc/resolv.conf'
TZ_CONF='/etc/localtime'

conf_variable_write() {
    /bin/echo -n ${2} >> ${1}
    /bin/echo -n "="  >> ${1}
    /bin/echo -n "'"  >> ${1}
    /bin/echo -n ${3} >> ${1}
    /bin/echo    "'"  >> ${1}

    return 0
}

case "${1}" in
    deconfig)

        # If we have an IP address, then do not reset the interface, because
        # (1) resetting the interface on NFS boot is fatal and
        # (2) resetting the interface on DHCP restart is not needed.
        IP_ADDRESS=`/sbin/ifconfig ${interface} | /bin/grep '^ *inet addr:' | /bin/sed 's%^ *inet addr:\([^ ]*\) .*%\1%'`
        if /usr/bin/test -z "${IP_ADDRESS}" ; then
            /sbin/ifconfig ${interface} 0.0.0.0 broadcast 255.255.255.255 up
        fi

        # Remove configuration generated by this script.
        while /sbin/route del default gw 0.0.0.0 dev ${interface} 2> /dev/null ; do
            :
        done
        /bin/hostname ""
        /bin/rm -f "${DHCP_CONF}"
        /bin/rm -f "${HOSTS_CONF}"
        /bin/rm -f "${LOG_CONF}"
        /bin/rm -f "${NTP_CONF}"
        /bin/rm -f "${RESOLV_CONF}"
        /bin/rm -f "${TZ_CONF}"

        ;;
    bound|renew)
        echo "udhcpd enviroment from DHCP server:"
        echo "hostname:${hostname}"
        echo "domain  :${domain}"
        echo "tzdbstr :${tzdbstr}"
        echo "dns     :${dns}"
        echo "ntpsrv  :${ntpsrv}"
        echo "logsrv  :${logsrv}"

        # Perform DHCP overrides.
        hostname=${MM_DHCP_HOST_NAME:-${hostname}}
        domain=${MM_DHCP_DOMAIN_NAME:-${domain}}
        tzdbstr=${MM_DHCP_TCODE:-${tzdbstr}}
        dns=${MM_DHCP_DOMAIN_NAME_SERVERS:-${dns}}
        ntpsrv=${MM_DHCP_NTP_SERVERS:-${ntpsrv}}
        logsrv=${MM_DHCP_LOG_SERVERS:-${logsrv}}

        echo "current udhcpd enviroment(with overrides):"
        echo "hostname:${hostname}"
        echo "domain  :${domain}"
        echo "tzdbstr :${tzdbstr}"
        echo "dns     :${dns}"
        echo "ntpsrv  :${ntpsrv}"
        echo "logsrv  :${logsrv}"

        # Configure interface basics.
        /usr/bin/test -n "${broadcast}" && BROADCAST="broadcast ${broadcast}"
        /usr/bin/test -n "${subnet}"    && NETMASK="netmask ${subnet}"
        /sbin/ifconfig ${interface} ${ip} ${BROADCAST} ${NETMASK}

        # Configure default gateways.
        while /sbin/route del default gw 0.0.0.0 dev ${interface} 2> /dev/null ; do
            :
        done
        for GATEWAY in ${router} ; do
            /sbin/route add default gw ${GATEWAY} dev ${interface}
        done

        # Set host name.
        /bin/hostname "${hostname}"

        # Create local hosts file.
        /bin/rm -f ${HOSTS_CONF}~
        /bin/touch ${HOSTS_CONF}~
        /bin/chmod 644 ${HOSTS_CONF}~
        /bin/echo "127.0.0.1 localhost.localdomain localhost" >> ${HOSTS_CONF}~
        if /usr/bin/test -n "${hostname}" ; then
            /bin/echo -n "${ip}"                              >> ${HOSTS_CONF}~
            if /usr/bin/test -n "${domain}" ; then
                /bin/echo -n " ${hostname}.${domain}"         >> ${HOSTS_CONF}~
            fi
            /bin/echo -n " ${hostname}"                       >> ${HOSTS_CONF}~
        fi
        /usr/bin/cmp -s "${HOSTS_CONF}~" "${HOSTS_CONF}" 2> /dev/null
        if /usr/bin/test $? -ne 0 ; then
            /bin/mv -f ${HOSTS_CONF}~ ${HOSTS_CONF}
        else
            /bin/rm -f ${HOSTS_CONF}~
        fi

        # Set time zone.
        if   /usr/bin/test -z "${tzdbstr}" ; then
            /bin/rm -f ${TZ_CONF}
        elif /usr/bin/test ! -r /usr/share/zoneinfo/${tzdbstr} ; then
            /bin/rm -f ${TZ_CONF}
            /usr/bin/logger -s -t udhcpc -p "daemon.err" \
                "error: tzdbstr (MM_DHCP_TCODE) '${tzdbstr}' not valid."
        else
            /bin/rm -f ${TZ_CONF}~
            /bin/touch ${TZ_CONF}~
            /bin/chmod 644 ${TZ_CONF}~
            /bin/cat /usr/share/zoneinfo/${tzdbstr} > ${TZ_CONF}~
            /usr/bin/cmp -s "${TZ_CONF}~" "${TZ_CONF}" 2> /dev/null
            if /usr/bin/test $? -ne 0 ; then
                /bin/mv -f ${TZ_CONF}~ ${TZ_CONF}
            else
                /bin/rm -f ${TZ_CONF}~
            fi
        fi

        # Create domain name resolution file.
        /bin/rm -f ${RESOLV_CONF}~
        /bin/touch ${RESOLV_CONF}~
        /bin/chmod 644 ${RESOLV_CONF}~
        if /usr/bin/test -n "${domain}" ; then
            /bin/echo search ${domain} >> ${RESOLV_CONF}~
        fi
        for DOMAIN_NAME_SERVER in ${dns} ; do
            if /usr/bin/test -n "${DOMAIN_NAME_SERVER}" ; then
                /bin/echo nameserver ${DOMAIN_NAME_SERVER} >> ${RESOLV_CONF}~
            fi
        done
        /usr/bin/cmp -s "${RESOLV_CONF}~" "${RESOLV_CONF}" 2> /dev/null
        if /usr/bin/test $? -ne 0 ; then
            /bin/mv -f ${RESOLV_CONF}~ ${RESOLV_CONF}
        else
            /bin/rm -f ${RESOLV_CONF}~
        fi

        # Create NTP configuration file.
        /bin/rm -f ${NTP_CONF}~
        /bin/touch ${NTP_CONF}~
        /bin/chmod 644 ${NTP_CONF}~
        for NTP_SERVER in ${ntpsrv}; do
            if /usr/bin/test -n "${NTP_SERVER}" ; then
                /bin/echo server ${NTP_SERVER} >> ${NTP_CONF}~
            fi
        done
        /usr/bin/cmp -s "${NTP_CONF}~" "${NTP_CONF}" 2> /dev/null
        if /usr/bin/test $? -ne 0 ; then
            /bin/mv -f ${NTP_CONF}~ ${NTP_CONF}
        else
            /bin/rm -f ${NTP_CONF}~
        fi

        # Create log server configuration file.
        /bin/rm -f ${LOG_CONF}~
        /bin/touch ${LOG_CONF}~
        /bin/chmod 644 ${LOG_CONF}~
        for LOG_SERVER in ${logsrv}; do
            if /usr/bin/test -n "${LOG_SERVER}" ; then
                /bin/echo server ${LOG_SERVER} >> ${LOG_CONF}~
            fi
        done
        /usr/bin/cmp -s "${LOG_CONF}~" "${LOG_CONF}" 2> /dev/null
        if /usr/bin/test $? -ne 0 ; then
            /bin/mv -f ${LOG_CONF}~ ${LOG_CONF}
        else
            /bin/rm -f ${LOG_CONF}~
        fi

        # Create MiniMyth DHCP configuration file.
        /bin/rm -f "${DHCP_CONF}~"
        /bin/touch ${DHCP_CONF}~
        /bin/chmod 644 ${DHCP_CONF}~
        NETWORK_INTERFACE=${interface}
        DHCP_SERVER=${serverid}
        TFTP_SERVER=${siaddr}
        TFTP_BOOT_FILE=${boot_file}
        TFTP_BOOT_URL=
        if /usr/bin/test ! "x${TFTP_BOOT_FILE}" = "x" ; then
            if /bin/echo ${TFTP_BOOT_FILE} | /bin/grep -q '://' ; then
                TFTP_BOOT_URL=`/bin/echo "${TFTP_BOOT_FILE}" | /bin/sed -e 's%[^/]*$%%' -e 's%[^/]*/$%%'`
            else
                if /usr/bin/test ! "x${TFTP_SERVER}" = "x" ; then
                    TFTP_BOOT_DIR=`/bin/echo "/${TFTP_BOOT_FILE}" | /bin/sed -e 's%[^/]*$%%' -e 's%//*%/%g'`
                    TFTP_BOOT_URL="tftp://${TFTP_SERVER}${TFTP_BOOT_DIR}"
                fi
            fi
        fi
        conf_variable_write ${DHCP_CONF}~ MM_NETWORK_INTERFACE "${NETWORK_INTERFACE}"
        conf_variable_write ${DHCP_CONF}~ MM_DHCP_SERVER       "${DHCP_SERVER}"
        conf_variable_write ${DHCP_CONF}~ MM_TFTP_SERVER       "${TFTP_SERVER}"
        conf_variable_write ${DHCP_CONF}~ MM_TFTP_BOOT_URL     "${TFTP_BOOT_URL}"
        /usr/bin/cmp -s "${DHCP_CONF}~" "${DHCP_CONF}" 2> /dev/null
        if /usr/bin/test $? -ne 0 ; then
            /bin/mv -f ${DHCP_CONF}~ ${DHCP_CONF}
        else
            /bin/rm -f ${DHCP_CONF}~
        fi

        ;;
esac

exit 0
